import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * @date 2018-09-28

 */
import { getResponse, createPagination } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { fetchTableList, createRuleEngine as _createRuleEngine, editRuleEngine as _editRuleEngine, deleteRuleEngine as _deleteRuleEngine, searchDetail, testRuleEngine as _testRuleEngine } from "../services/ruleEngineService";
export default {
  namespace: 'ruleEngine',
  state: {
    ruleEngineData: {},
    // 查询数据列表
    scriptTypeCode: [],
    // 脚本类型
    detail: {},
    // 详情数据
    testContent: undefined,
    // 测试结果
    pagination: {},
    // 分页信息
    categoryList: [] // 脚本分类值集

  },
  effects: {
    // 获取表格数据
    queryTableList: /*#__PURE__*/_regeneratorRuntime.mark(function queryTableList(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function queryTableList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(fetchTableList, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  ruleEngineData: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, queryTableList);
    }),
    // 获取脚本类型
    queryScriptTypeCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryScriptTypeCode(_, _ref3) {
      var call, put, scriptTypeCode;
      return _regeneratorRuntime.wrap(function queryScriptTypeCode$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref3.call, put = _ref3.put;
              _context2.t0 = getResponse;
              _context2.next = 4;
              return call(queryIdpValue, 'CPFM.RULE_SCRIPT_TYPE');

            case 4:
              _context2.t1 = _context2.sent;
              scriptTypeCode = (0, _context2.t0)(_context2.t1);
              _context2.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  scriptTypeCode: scriptTypeCode
                }
              });

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, queryScriptTypeCode);
    }),
    // 获取脚本分类值集
    queryCategoryList: /*#__PURE__*/_regeneratorRuntime.mark(function queryCategoryList(_, _ref4) {
      var call, put, categoryList;
      return _regeneratorRuntime.wrap(function queryCategoryList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              call = _ref4.call, put = _ref4.put;
              _context3.t0 = getResponse;
              _context3.next = 4;
              return call(queryIdpValue, 'CPFM.RULE_SCRIPT_CATEGORY');

            case 4:
              _context3.t1 = _context3.sent;
              categoryList = (0, _context3.t0)(_context3.t1);
              _context3.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  categoryList: categoryList
                }
              });

            case 8:
            case "end":
              return _context3.stop();
          }
        }
      }, queryCategoryList);
    }),
    // 编辑获取头
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref5, _ref6) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context4.next = 4;
              return call(searchDetail, payload);

            case 4:
              result = _context4.sent;
              result = getResponse(result);
              _context4.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  detail: result
                }
              });

            case 8:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchDetail);
    }),
    // 新建保存
    createRuleEngine: /*#__PURE__*/_regeneratorRuntime.mark(function createRuleEngine(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createRuleEngine$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context5.t0 = getResponse;
              _context5.next = 5;
              return call(_createRuleEngine, _objectSpread({}, payload));

            case 5:
              _context5.t1 = _context5.sent;
              res = (0, _context5.t0)(_context5.t1);
              return _context5.abrupt("return", getResponse(res));

            case 8:
            case "end":
              return _context5.stop();
          }
        }
      }, createRuleEngine);
    }),
    // 编辑保存
    editRuleEngine: /*#__PURE__*/_regeneratorRuntime.mark(function editRuleEngine(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function editRuleEngine$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(_editRuleEngine, _objectSpread({}, payload));

            case 5:
              _context6.t1 = _context6.sent;
              res = (0, _context6.t0)(_context6.t1);
              return _context6.abrupt("return", getResponse(res));

            case 8:
            case "end":
              return _context6.stop();
          }
        }
      }, editRuleEngine);
    }),
    // 删除
    deleteRuleEngine: /*#__PURE__*/_regeneratorRuntime.mark(function deleteRuleEngine(_ref11, _ref12) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteRuleEngine$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context7.t0 = getResponse;
              _context7.next = 5;
              return call(_deleteRuleEngine, payload);

            case 5:
              _context7.t1 = _context7.sent;
              res = (0, _context7.t0)(_context7.t1);
              return _context7.abrupt("return", getResponse(res));

            case 8:
            case "end":
              return _context7.stop();
          }
        }
      }, deleteRuleEngine);
    }),
    // 测试
    testRuleEngine: /*#__PURE__*/_regeneratorRuntime.mark(function testRuleEngine(_ref13, _ref14) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function testRuleEngine$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call, put = _ref14.put;
              _context8.next = 4;
              return call(_testRuleEngine, _objectSpread({}, payload));

            case 4:
              result = _context8.sent;
              result = getResponse(result);

              if (!result) {
                _context8.next = 9;
                break;
              }

              _context8.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  testContent: result.content
                }
              });

            case 9:
              return _context8.abrupt("return", result);

            case 10:
            case "end":
              return _context8.stop();
          }
        }
      }, testRuleEngine);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};